<?php

namespace app\wx\controller;

use think\Db;
use think\facade\Request;
use think\Validate;
use think\helper\Time;

class Daywork extends Base
{
    protected $beforeActionList = [
        'first',
    ];
    protected $uid = '';

    protected function first()
    {
        if (session('uid') || input('openid')) {
            if (session('uid')) {
                $this->uid = session('uid');
            } else {
                $this->uid = DB::name('admin')->where('openid', input('openid'))->value('id');
                if($this->uid){
                    session('uid', $this->uid);
                }else{
                    $url=url('/wx/interest/index' , '', '', Request::host());
                    $this->redirect($url);
                }
            }
        } else {
            $url = url('Daywork/index', '', '', Request::host());
            $this->redirect('https://openwx.weibuyun.com/wxuser/get_openid' . '?domain=' . Request::host() . '&url=' . $url);
        }
    }
    public function index()
    {

            return view();
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        if (empty($post['title']) || $post['title'] == '') {
        } else {
            $map[] = ['title', 'like', '%' . $post['title'] . '%'];
        }

        $map[] = ['work_time.uid', '=', session('uid')];

        $data['count'] = Db::view('work_time', 'id,c_time,uid,w_time,project_id,stories_id')
            ->view('admin', 'nickname', 'work_time.uid=admin.id')
            ->view('project', 'title', 'work_time.project_id=project.id')
            ->view('stories', ['title' => 'stories_title'], 'work_time.stories_id=stories.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('work_time', 'id,c_time,uid,w_time,project_id')
            ->view('admin', 'nickname', 'work_time.uid=admin.id')
            ->view('project', 'title', 'work_time.project_id=project.id')
            ->view('stories', ['title' => 'stories_title'], 'work_time.stories_id=stories.id')
            ->where($map)
            // ->fetchSql(true)
            ->order('work_time.id desc')->page(input("param.page"), input('param.limit'))->select();
        foreach ($data['data'] as &$info) {
            $info['c_time'] = date('m-d H:i:s', $info['c_time']);
            $info['title'] = '[' . $info['title'] . ']' . $info['stories_title'];
        }
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

}
